package com.soboot.system.controller;

import com.soboot.base.converter.BeanMapping;
import com.soboot.base.model.BaseController;
import com.soboot.response.Result;
import com.soboot.system.entity.SysMenu;
import com.soboot.system.model.MenuSaveReqModel;
import com.soboot.system.service.ISysMenuService;
import com.soboot.system.vo.MenuVO;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:
 * @createTime: 2022年12月06日 14:35:48
 * @version:
 * @Description:
 * @Copyright:
 */
@RestController
@RequestMapping("/menus")
@Api(value="菜单管理接口", tags = {"菜单管理"})
public class MenuController extends BaseController {

    @Autowired
    private ISysMenuService menuService;

    @ApiOperation(value = "列表", notes = "")
    @GetMapping("/list")
    public Result<List<MenuVO>> list() {

        List<MenuVO> result = menuService.getList();
        return Result.success(result);
    }

    @ApiOperation(value = "详细信息", notes = "详细信息")
    @ApiImplicitParam(name = "id", value = "资源ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<MenuVO> detail(@PathVariable Integer id) {

        MenuVO result = menuService.detail(id);
        return Result.success(result);
    }

    @ApiOperation(value = "添加", notes = "添加")
    @ApiImplicitParam(name = "menu", value = "资源详细实体menu", required = true, dataType = "MenuDto")
    @PostMapping()
    public Result<String> addMenu(@RequestBody @ApiParam @Validated({Insert.class}) MenuSaveReqModel reqModel) {

        SysMenu sysMenu = BeanMapping.map(reqModel,SysMenu.class);
        menuService.saveOrUpdate(sysMenu);
        return Result.success();
    }

    @ApiOperation(value = "更新", notes = "更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资源ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "menu", value = "资源详细实体menu", required = true, dataType = "MenuDto")
    })
    @PutMapping("/{id}")
    public Result<String> updateMenu(@PathVariable Integer id, @RequestBody @ApiParam @Validated({Update.class}) MenuSaveReqModel reqModel) {

        SysMenu sysMenu = BeanMapping.map(reqModel,SysMenu.class);
        menuService.saveOrUpdate(sysMenu);
        return Result.success();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @ApiImplicitParam(name = "id", value = "资源ID", required = true, dataType = "Integer")
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {

        menuService.delete(id);
        return Result.success();
    }
}
